package com.retrozelda.nyanimals;

import android.opengl.GLES20;
import android.opengl.Matrix;
import android.util.Log;
import android.view.MotionEvent;

	public class CNyanimalGame
	{
		private static final String TAG = CNyanimalGame.class.getSimpleName();
		private float[]			m_VPMatrix		= new float[16];
		private float[]			m_ProjMatrix	= new float[16];
		private float[]			m_VMatrix		= new float[16];
		private long			m_curTime;
		private long			m_prevTime;
		private long			m_deltaTime;
		

		CRoom	m_Room = new CRoom();

		private volatile boolean m_Initialized = false;
		private volatile Boolean m_RenderLock = false;
		
		public boolean ShouldExit()
		{
			return m_Room.ShouldExit();
		}
		
		
		public float[] GetViewProjection()
		{
			return m_VPMatrix;
		}
	
		public float[] GetView()
		{
			return m_VMatrix;
		}
	
		public float[] GetProjection()
		{
			return m_ProjMatrix;
		}
	
		public void Init(NyanimalGLSurfaceView game)
		{

			// set the view matrix
			Matrix.setLookAtM(m_VMatrix, 0, 0, 0.0f, -1f, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

			// set the game stuff
			m_Room.Init(game);
			m_prevTime = System.currentTimeMillis();
			m_Initialized = true;
		}
	

	public void onSurfaceChanged(int width, int height)
	{
			Log.d(TAG, "Surface Changed!");
			// set the projection matrix
			GLES20.glViewport(0, 0, width, height);
			// float ratio = (float) width / height;
			// Matrix.frustumM(m_ProjMatrix, 0, -ratio, ratio, -1, 1, 1, 9);
			Matrix.orthoM(m_ProjMatrix, 0, 0, -width, height, 0, -1, 1);
	
			// calculate the view proj matrix
			Matrix.multiplyMM(m_VPMatrix, 0, m_ProjMatrix, 0, m_VMatrix, 0);
			
			// pass the width and height
			m_Room.UpdateScreenData(width, height);
	}
	
	
	public void Update()
	{
			if(m_Initialized == false)
			{
				return;
			}
			// update the game stuff
			m_curTime = System.currentTimeMillis();
			m_deltaTime = m_curTime - m_prevTime;
			m_prevTime = m_curTime;

			synchronized(m_RenderLock)
			{
				//Log.d(TAG + ":Update()", "Update Lock");
				m_Room.Update((float) m_deltaTime / 1000);
				//Log.d(TAG + ":Update()", "Update UnLock");
			}
	}

	public void Render(NyanimalGLSurfaceView game)
	{
			if(m_Initialized == false)
			{
				return;
			}
			
			// draw our game stuff
			synchronized(m_RenderLock)
			{
				// Log.d(TAG + ":Render()", "Render Lock");
				
				// Ignore the passed-in GL10 interface, and use the GLES20
				// class's static methods instead.
				GLES20.glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
				GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
	
				
				// build the argument struct
				CArgumentGroup args = new CArgumentGroup();
				args.GameClass = this;
				args.GLSurface = game;
				m_Room.Draw(args);
				
				//Log.d(TAG + ":Render()", "Render UnLock");
			}
	}
	
	public boolean TouchEvent(MotionEvent event)
	{ 
		//Log.d(TAG + ":Touch()", "Touch Lock");
		boolean result = true;
		synchronized(m_RenderLock)
		{
			result = m_Room.HandleTouchEvent(event);
		}
		//Log.d(TAG + ":Touch()", "Touch UnLock");
		
		return result;
	}
};